package sg.edu.nus.iss.billing.parse;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import sg.edu.nus.iss.billing.entity.PaymentHistory;
import sg.edu.nus.iss.billing.entity.transaction.CallTransaction;
import sg.edu.nus.iss.billing.entity.transaction.CallType;
/**
 * @author sammil
 *
 */
public class PaymentHistoryParser extends  Parser {

	public static final PaymentHistoryParser SINGLETON = new PaymentHistoryParser();
	private static final int MAX_LENGTH = 2 * Parser.MAX_LINE_LEN;
	
	private PaymentHistoryParser(){
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public <L> boolean parse(LineNumberReader in, HashMap<String,ArrayList<L>> outputMap) throws IOException {
		// TODO Auto-generated method stub

		try {
		in.mark(MAX_LENGTH);
		//System.out.println("\nStarted reading Payment History record from line "	+ in.getLineNumber());

		StringReader line = nextLine(in);
		String paymentRecord = readBufVar(line, MAX_LINE_LEN).trim();
		//System.out.println("paymentRecord :"	+ paymentRecord);
		if(paymentRecord.equals("")) {
			//System.out.println("paymentRecord : EMPTY RECORD"	);
			// we've hit blank space, which means we're now reading
			// a footer record not a transaction
			//in.reset();
			return false;
		}
		if(paymentRecord.equals("END OF TRANSACTION")) {
			//System.out.println("paymentRecord : END OF TRANSACTION"	);
			// we've hit blank space, which means we're now reading
			// a footer record not a transaction
			in.reset();
			return false;
		}
		
		if(paymentRecord.contains(PaymentHeaderParser.PAYMENT_HEADER_LINE)) {
			//System.out.println("paymentRecord : NUMBER"	);
			// we've hit a page header record not a transaction
			in.reset();
			return false;
		}
		
		if(paymentRecord.contains(AccountHeaderParser.PAGE_PAYMENT_TITLE)) {
			//System.out.println("paymentRecord : PAGE_PAYMENT_TITLE"	);
			// we've hit a page header record not a transaction
			in.reset();
			return false;
		}
		
		String paymentInfo[] = paymentRecord.split("\t\t");
		//System.out.println("callInfo : " + paymentInfo.length	);
		
		if(paymentInfo.length == 3){
			String dateofPayment = paymentInfo[0];
			String amount = paymentInfo[1];
			String paymentMode = paymentInfo[2];
			PaymentHistory paymentHistory = new PaymentHistory(Util.toDate(dateofPayment), new BigDecimal(amount),paymentMode);
			outputMap.get(AccountHeaderParser.accountNo).add((L) paymentHistory);
		}else {
			String error = "Failed to get the Payment Informaton from Payment Record at line no : " + in.getLineNumber();
			throw new RuntimeException(error);
		}
		
		} catch (IOException e) {

			System.err.println("PaymentHistoryParser"+ e);
			// go back to where we started
			in.reset();
			// re-throw the exception
			throw e;
		} catch (RuntimeException e) {

			System.out.println("Payment History record Not available at line "
					+ in.getLineNumber() + ": " + e.getMessage());
			in.reset();
			return false;
		}
		System.out.println("Parsed  PaymentHistory  record");
		return true;
		
	}

}
